package com.game.view.layer
{
	import com.dusk.tool.Time;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.engine.MainGame;
	import com.game.manager.GameSceneManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	public class GameWin extends LayerBase
	{
		
		public function GameWin()
		{
			super();
		}
		
		public var allScore:TextField;
		public var maxCombo:TextField;
		public var passState:TextField;
		public var passTime:TextField;
		public var nextStageBtn:SimpleButton;
		public var toMapBtn:SimpleButton;
		
		private var goTimer:int;
		private var ahp:Number;
		private var amp:Number;
		
		private var nextStage:int;
		private var nextLevel:int;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("GameWin") as MovieClip;
			TextUtil.redrawTxt(mc);
			allScore = mc.getChildByName("allScore") as TextField;
			passTime = mc.getChildByName("passTime") as TextField;
			maxCombo = mc.getChildByName("maxCombo") as TextField;
			passState = mc.getChildByName("passState") as TextField;
			nextStageBtn = mc.getChildByName("nextStageBtn") as SimpleButton;
			toMapBtn = mc.getChildByName("toMapBtn") as SimpleButton;
			addChild(mc);
		}
		
		private function setUserTime():void
		{
			this.goTimer = getTimer() - MainGame.getIns().battleData.startTime - MainGame.getIns().battleData.pauseTotal;
			if (this.goTimer <= 0)
			{
				this.passTime.text = "99999";
			}
			else
			{
				var time:Time = new Time(this.goTimer / 1000);
				this.passTime.text = time.toTimeString();
			}
		}
		
		private function setAllScore():void
		{
			var _loc1_:int = (5 * 60 + this.goTimer / 1000) / (this.goTimer / 1000 * 50);
			if (_loc1_ > 800)
			{
				_loc1_ = 800;
			}
			var _loc2_:int = 300 * (this.amp + this.ahp);
			if (_loc2_ > 5 * 60)
			{
				_loc2_ = 5 * 60;
			}
			var _loc3_:* = MainGame.getIns().battleData.maxCombo;
			if (_loc3_ > 800)
			{
				_loc3_ = 800;
			}
			var _loc4_:Number = _loc1_ + _loc2_ + _loc3_;
			this.allScore.text = _loc4_.toString();
		}
		
		private function setState():void
		{
			this.amp = 0;
			this.ahp = 0;
			if (this.gc.team.hero)
			{
				this.amp = this.gc.team.hero.getMp() / this.gc.team.hero.getMpMax() / 2;
				this.ahp = this.gc.team.hero.getHp() / this.gc.team.hero.getHpMax() / 2;
			}
			this.passState.text = int((this.amp + this.ahp) * 100) + "%";
			if (int((this.amp + this.ahp) * 100) > 100)
			{
				this.passState.text = "100%";
			}
		}
		
		private function setHigh():void
		{
			this.maxCombo.text = MainGame.getIns().battleData.maxCombo.toString();
		}
		
		override protected function onAdded():void
		{
			if(MainGame.getIns().levelData.getNextLevelIDArray() != null)
			{
				this.nextStage = MainGame.getIns().levelData.getNextLevelIDArray()[0];
				this.nextLevel = MainGame.getIns().levelData.getNextLevelIDArray()[1];
				this.nextStageBtn.visible = true;
				this.nextStageBtn.addEventListener(MouseEvent.CLICK, this.nextClick);
			}
			else
			{
				this.nextStageBtn.visible = false;
			}
			this.toMapBtn.visible = true;
			this.toMapBtn.addEventListener(MouseEvent.CLICK, backClick);
			this.setUserTime();
			this.setHigh();
			this.setState();
			this.setAllScore();
		}
		
		override protected function onRemoved():void
		{
			this.nextStageBtn.removeEventListener(MouseEvent.CLICK, nextClick);
			this.toMapBtn.removeEventListener(MouseEvent.CLICK, backClick);
		}
		
		private function nextClick(param1:MouseEvent):void
		{
			removeFromParent();
			GameSceneManager.getIns().switchScene(GameSceneManager.START_FIGHTING,nextStage + "_" + nextLevel);
		}
		
		private function backClick(param1:MouseEvent):void
		{
			removeFromParent();
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
		}
	}
}
